package com.cm.etax.tianjin;

import com.cm.etax.BaseEtax;
import com.cm.etax.BaseFunction;
import com.cm.etax.tianjin.bean.SessionUser;
import com.cm.kits.JacksonKits;
import org.openqa.selenium.By;

/**
 * 会话内容管理方法
 *
 * @author LWang 2023.01.29
 * @since 1.0.0
 */
public class SessionFunction extends BaseFunction {
    public SessionFunction(BaseEtax etax) {
        super(etax);
    }

    /**
     * 获取当前登录用户信息
     *
     * @param refresh 是否刷新页面
     * @return 登录用户信息
     */
    public SessionUser getSessionUser(boolean refresh) {
        try {
            if (refresh) {
                rpa().refresh();
                rpa().sleep(SLEEP_TIME);
            }
            rpa().waitElement(By.cssSelector("div.user span.userName span.userNameInfo"), WAIT_TIME);
            var val = rpa().executeJavaScript("""
                    return (function() {
                        try {
                            return JSON.stringify(WSSW.sessionUser);
                        } catch(e) {
                            return "{}";
                        }
                    })();
                    """, String.class);
            return JacksonKits.toBean(val, SessionUser.class);
        } catch (Exception ignore) {
        }
        return null;
    }
}
